dp hashing strings *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define all(x) (x).begin(), (x).end()
const int MAXN = 5005;

int dp[MAXN][MAXN];
int is[MAXN][MAXN];

void fill()
{
	for(int i = 0; i <= 5001; i++)
	{
		for(int j = 0; j <= 5001; j++)
			is[i][j] = 1;
	}
}

int32_t main()
{
	fill();
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    string str; cin >> str;
    int boyut = str.size();
    for(int i = boyut; i >= 1; i--)
    {
        for(int j = i; j <= boyut; j++)
        {
            if(i == j) continue;

            if(str[i -1] == str[j -1]) is[i][j] = is[i +1][j -1];
            else is[i][j] = 0;
        }
    }

    for(int i = boyut; i >= 1; i--)
    {
        for(int j = i; j <= boyut; j++)
        {
            if(i == j)
            {
                dp[i][j] = 1;
                continue;
            }

            dp[i][j] = is[i][j];
            dp[i][j] += dp[i +1][j] + dp[i][j -1] - dp[i +1][j -1];
        }
    }

    int test; cin >> test;
    while(test--)
    {
        int l, r; cin >> l >> r;
        cout << dp[l][r] << "\n";
    }
 
    return 0;
}


Comments

Submit
0 Comments
More Questions

1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements